package com.kurumi.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kurumi.pojo.RespondResult;
import com.kurumi.query.MedicalRecordPaginationQuery;
import com.kurumi.service.MedicalRecordPaginationService;
import com.kurumi.service.MedicalRecordService;
import com.kurumi.util.JsonUtil;
import com.kurumi.util.StringUtil;

@Controller
@RequestMapping("/medical_record_pagination")
public class MedicalRecordPaginationController {

	@Autowired
	private MedicalRecordService medicalRecordService;
	
	@Autowired
	private MedicalRecordPaginationService medicalRecordPaginationService;
	
	@GetMapping("/ajax_query_pagination")
	@ResponseBody
	public RespondResult ajaxQueryPagination(MedicalRecordPaginationQuery params){
		RespondResult respondResult = null;
		
		try{
			List<Map<String,Object>> medicalRecords = new ArrayList<Map<String,Object>>();
			int count = 0;
			if(!params.empty()){
				medicalRecords = medicalRecordPaginationService.getMedicalRecordOfPagination(params);
				count= medicalRecordPaginationService.getMedicalRecordCountOfPagination(params);
			}
			
			params.setTotalCounts(count);
			params.setQueryDatas(medicalRecords);
			respondResult = new RespondResult(true, RespondResult.successCode, null, params);
		}catch (Exception e) {
			// TODO: handle exception
			respondResult = new RespondResult(false, RespondResult.errorCode, e.getMessage(), params);
		}
		
		return respondResult;
	}
	
	@GetMapping("/pagination_page")
	public String paginationPage(String visitGuid,Model model,HttpServletRequest request){
		List<Map<String,Object>> medicalRecords = medicalRecordService.getMedicalRecordByVisitGuid(visitGuid);
		if(medicalRecords.size() == 0){
			RespondResult respondResult = new RespondResult(false,"404","网址错误，资源不存在",null);
			model.addAttribute("respondResultJson", JsonUtil.objectToJson(respondResult));
		}else{
			Map<String,Object> medicalRecord = medicalRecords.get(0);
			RespondResult respondResult = new RespondResult(true,"302","",medicalRecord);
			model.addAttribute("respondResultJson", JsonUtil.objectToJson(respondResult));
		}
		
		List<Map<String,Object>> scanFiles = medicalRecordPaginationService.getUnPaginationImagesByVisitGuid(visitGuid);
		/*List<Map<String,Object>> scanFiles = medicalRecordScanService.getPaginationScanFilesByFirstGuidAndPagination(visitGuid,paginationStatus);
		*/model.addAttribute("scanFiles", scanFiles);

		//编页标签
		List<Map<String,Object>> mrPageTypes = medicalRecordPaginationService.getPaginationInfoByVisitGuid(visitGuid);
		model.addAttribute("mrPageTypes", mrPageTypes);
		model.addAttribute("visitGuid", visitGuid);
		return "pagination/pagination_page";
	}
	
	@ResponseBody
	@GetMapping("/finish_pagination_images_info")
	public RespondResult finishPaginationImagesInfo(String visitGuid){
		RespondResult respondResult = null;
		try{
			List<Map<String,Object>> scanFiles = medicalRecordPaginationService.getFinishPaginationImagesByVisitGuid(visitGuid);
			respondResult = new RespondResult(true, RespondResult.successCode, null, scanFiles);
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			respondResult = new RespondResult(false, RespondResult.errorCode, null, null);
		}
	 	return respondResult;
	}
	
	@ResponseBody
	@GetMapping("/un_pagination_images_info")
	public RespondResult unPaginationImagesInfo(String visitGuid){
		RespondResult respondResult = null;
		try{
			List<Map<String,Object>> scanFiles = medicalRecordPaginationService.getUnPaginationImagesByVisitGuid(visitGuid);
			respondResult = new RespondResult(true, RespondResult.successCode, null, scanFiles);
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			respondResult = new RespondResult(false, RespondResult.errorCode, null, null);
		}
	 	return respondResult;
	}
	
	
	
	@ResponseBody
	@PostMapping("/image_pagination")
	public RespondResult imagePagination(String visitGuid,Integer scanImageId,String newPageTypeCode,String newPageTypeName,String oldPageTypeCode, Model model){
		RespondResult respondResult = null;
		try{
			newPageTypeCode = StringUtil.empty(newPageTypeCode);
			oldPageTypeCode = StringUtil.empty(oldPageTypeCode);
			int result = medicalRecordPaginationService.imagePagination(scanImageId, newPageTypeCode,newPageTypeName);
			if(result == 1){
				List<Map<String,Object>> mrPageTypesOfPaginations = medicalRecordPaginationService.getPaginationInfoByVisitGuid(visitGuid);
				Map<String, Integer> newPaginationCountInfo = new HashMap<String, Integer>();
				for (Map<String, Object> mrPageTypesOfPagination : mrPageTypesOfPaginations) {
					String pageTypeCode = (String)mrPageTypesOfPagination.get("pageTypeCode");
					Long paginationCount = (Long)mrPageTypesOfPagination.get("count");
					if(newPageTypeCode!= null && pageTypeCode.equalsIgnoreCase(newPageTypeCode)){
						newPaginationCountInfo.put(newPageTypeCode, paginationCount.intValue());
					}else if(oldPageTypeCode!= null && pageTypeCode.equalsIgnoreCase(oldPageTypeCode)){
						newPaginationCountInfo.put(oldPageTypeCode, paginationCount.intValue());
					}
				}
				respondResult = new RespondResult(true, RespondResult.successCode, null, newPaginationCountInfo);
			}else{
				respondResult = new RespondResult(true, RespondResult.errorCode, "未预期错误", null);
			}
		}catch (Exception e) {
			// TODO: handle exception
			respondResult = new RespondResult(false, RespondResult.errorCode, "病案图片编页失败", null);
			e.printStackTrace();
		}
		return respondResult;
	}
	
	
	@ResponseBody
	@PostMapping("/batch_image_pagination")
	public RespondResult batchImagePagination(Integer[] scanImageIds, String mrPageTypeCode, String mrPageTypeName) {
		RespondResult respondResult = null;
		
		try {
			int result = medicalRecordPaginationService.batchImagePagination(scanImageIds, mrPageTypeCode,mrPageTypeName);
			
			/*Set<Integer> mrPageTypesSet = medRecordService.batchPaginationMedrecordImage(medicalResourceJsonStr, mrPageTypeId, firstGuid);
			List<Integer> mrPageTypes = new ArrayList<Integer>();
			
			for (Integer pagetTypeId : mrPageTypesSet) {
				mrPageTypes.add(pagetTypeId);
			}
			
			//编页标签的编页数量
			List<Map<String, Object>> resultMap = mrPageTypeService.getMrPageTypesOfPaginationCountByFirstGuidAndMrPageTypes(firstGuid, mrPageTypes);
			Map<Integer, Map<String,Object>> mrPageTypesOfPaginationMap = new HashMap<Integer, Map<String,Object>>();
			
			for (Map<String, Object> mrPageTypeOfPagination : resultMap) {
				mrPageTypesOfPaginationMap.put((Integer)mrPageTypeOfPagination.get("mr_page_type"), mrPageTypeOfPagination);
			}*/
			if(result == 1){
				respondResult = new RespondResult(true, RespondResult.successCode, null, "批量编页成功");
			}
			else{
				respondResult = new RespondResult(true, RespondResult.errorCode, "未预期错误", null);
			}
			
		} catch (Exception e) {
			respondResult = new RespondResult(false, RespondResult.errorCode, "病案图片编页失败", null);
			e.printStackTrace();
		}
		
		return respondResult;
		
	}
	
	@ResponseBody
	@GetMapping("/pagination_count_info")
	public RespondResult paginationCountInfo(String visitGuid){
		RespondResult respondResult = null;
		try{
			Map<String, Integer> paginationCountInfo = medicalRecordPaginationService.getPaginationCountInfo(visitGuid);
			respondResult = new RespondResult(true, RespondResult.successCode, null, paginationCountInfo);
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			respondResult = new RespondResult(false, RespondResult.errorCode, null, null);
		}
	 	return respondResult;
	}
	
	@ResponseBody
	@PostMapping("/update_pagination_image_sort_index")
	public RespondResult updatePaginationImageSortIndex(Integer scanImageId,Integer sortIndex) {
		RespondResult respondResult = null;
		try {
			int count = medicalRecordPaginationService.updatePaginationImageSortIndex(scanImageId, sortIndex);
			if(count == 1){
				respondResult = new RespondResult(true, RespondResult.successCode, "修改序列成功", null);
			}else{
				respondResult = new RespondResult(true, RespondResult.errorCode, "未预期错误", null);
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
			respondResult = new RespondResult(false, RespondResult.errorCode, "修改序列失败", null);
			
		}
		return respondResult;
	}
	
	
	@ResponseBody
	@PostMapping("/remove_pagination_image")
	public RespondResult removePaginationImage(String visitGuid,Integer scanImageId, String currentPageTypeCode, Model model){
		RespondResult respondResult = null;
		try{
			currentPageTypeCode = StringUtil.empty(currentPageTypeCode);
			int count = medicalRecordPaginationService.removePaginationImage(scanImageId);
			if(count == 1){
				Map<String, Integer> newPaginationCountInfo = new HashMap<String, Integer>();
				
				if(currentPageTypeCode != null){
					List<Map<String,Object>> mrPageTypesOfPaginations = medicalRecordPaginationService.getPaginationInfoByVisitGuid(visitGuid);
					
					for (Map<String, Object> mrPageTypesOfPagination : mrPageTypesOfPaginations) {
						String pageTypeCode = (String)mrPageTypesOfPagination.get("pageTypeCode");
						Long paginationCount = (Long)mrPageTypesOfPagination.get("count");
						if(pageTypeCode.equalsIgnoreCase(currentPageTypeCode)){
							newPaginationCountInfo.put(currentPageTypeCode, paginationCount.intValue());
						}
					}
				}
				respondResult = new RespondResult(true, RespondResult.successCode, "删除病案图片成功", newPaginationCountInfo);
			}else{
				respondResult = new RespondResult(true, RespondResult.errorCode, "未预期错误", null);
			}
			
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			respondResult = new RespondResult(false, RespondResult.errorCode, "删除病案图片失败", null);
		}
		
		return respondResult;
		
	}
	
	
}
